package com.blb.eduadservice.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;


@Slf4j
@EnableScheduling
@Configuration
public class UpdateAdTaskConfig {

    @Scheduled(cron = "*/5 * * * * ?")
    public void updateAd() {
        try {
            //调用nginx中进行缓存更新的脚本
            URL url = new URL("http://192.168.114.200:8886/ad_update?space_id=1");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.setConnectTimeout(3000);
            int responseCode = con.getResponseCode();
            if(responseCode==200){
                 log.info("更新成功", LocalDateTime.now());
            }else{
                log.error("更新失败",LocalDateTime.now());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
